package com.asktgapp.engineer.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.asktgapp.R;
import com.asktgapp.application.ApiUtil;
import com.asktgapp.application.Apiservice;
import com.asktgapp.architecture.retrofit.ApiException;
import com.asktgapp.architecture.retrofit.ApiResponseBody;
import com.asktgapp.architecture.rxjava.ExceptionHandle;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.engineer.activity.FindWithDrawSuccessActivity;
import com.asktgapp.engineer.activity.ForgetWithDrawActivity;
import com.asktgapp.modulebase.common.util.PreferencesUtil;
import com.asktgapp.widget.InputView;
import com.asktgapp.modulebase.common.util.Util;


import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * time:2018/1/19
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description:5.9.4根据旧密码，修改提现密码
 */

public class EditWithDrawFragment extends BaseFragment {
    @InjectView(R.id.iv_first_eye)
    ImageView mFisrtEyeImg;
    @InjectView(R.id.iv_sencond_eye)
    ImageView mSencondEyeImg;
    @InjectView(R.id.iv_third_eye)
    ImageView mThirdEyeImg;
    @InjectView(R.id.input_first)
    InputView mFirstInputView;
    @InjectView(R.id.input_sencond)
    InputView mSencondInputView;
    @InjectView(R.id.input_third)
    InputView mThirdInputView;
    @InjectView(R.id.tv_sure)
    TextView mSure;
    @InjectView(R.id.tv_forget)
    TextView mForget;
    private String pwd1 = "";
    private String pwd2 = "";
    private String pwd3 = "";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_edit_withdraw, container, false);
        ButterKnife.inject(this, viewGroup);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
        getBaseActivity().setBackNavigationIcon();
        mFisrtEyeImg.setOnClickListener(this);
        mSencondEyeImg.setOnClickListener(this);
        mThirdEyeImg.setOnClickListener(this);
        mSure.setOnClickListener(this);
        mFirstInputView.setInputCallBack(new InputView.InputCallBack() {
            @Override
            public void onInputFinish(String pwd) {
                pwd1 = pwd;
            }
        });
        mSencondInputView.setInputCallBack(new InputView.InputCallBack() {
            @Override
            public void onInputFinish(String pwd) {
                pwd2 = pwd;
            }
        });
        mThirdInputView.setInputCallBack(new InputView.InputCallBack() {
            @Override
            public void onInputFinish(String pwd) {
                pwd3 = pwd;
            }
        });
        mForget.setOnClickListener(this);
    }

    @Override
    public void loadData() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_first_eye:
                if (mFirstInputView.getShowPassword()) {
                    mFisrtEyeImg.setImageResource(R.mipmap.ico_biiyan);
                } else {
                    mFisrtEyeImg.setImageResource(R.mipmap.ico_zhengyan);
                }
                mFirstInputView.changeShow();
                break;
            case R.id.iv_sencond_eye:
                if (mSencondInputView.getShowPassword()) {
                    mSencondEyeImg.setImageResource(R.mipmap.ico_biiyan);
                } else {
                    mSencondEyeImg.setImageResource(R.mipmap.ico_zhengyan);
                }
                mSencondInputView.changeShow();
                break;

            case R.id.iv_third_eye:
                if (mThirdInputView.getShowPassword()) {
                    mThirdEyeImg.setImageResource(R.mipmap.ico_biiyan);
                } else {
                    mThirdEyeImg.setImageResource(R.mipmap.ico_zhengyan);
                }
                mThirdInputView.changeShow();
                break;
            case R.id.tv_sure:
                setPassWord();

                break;

            case R.id.tv_forget:
                //跳转到根据手机验证码设置提现密码界面
                startActivity(new Intent(getActivity(), ForgetWithDrawActivity.class));

                break;
        }
    }

    private void setPassWord() {
        if (Util.isEmpty(pwd1)) {
            showTost("请输入原来的提现密码！", 1);
            return;
        }

        if (Util.isEmpty(pwd3) || Util.isEmpty(pwd2)) {
            showTost("请完善密码！", 1);
            return;
        }
        if (pwd3.equals(pwd2)) {
            // TODO: 2018/1/18 网络请求
            doRequest(pwd1, pwd2);

        } else {
            showTost("两次设置的密码不一致！", 1);
            pwd2 = "";
            pwd3 = "";
            mSencondInputView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mSencondInputView.clearResult();
                }
            }, 000);
            mThirdInputView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mThirdInputView.clearResult();
                }
            }, 200);
            return;
        }

    }

    private void doRequest(String oldPwd, String newPwd) {
        Apiservice apiservice = ApiUtil.getInstance().create();
        HashMap params = new HashMap();
        params.put("type", 2);
        params.put("newPwd", Util.encodeMD5(newPwd));
        params.put("oldPwd", Util.encodeMD5(oldPwd));
        params.put("userid", PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID));
        showProgress(getClass().getSimpleName());
        Call<ApiResponseBody> call = apiservice.updateNick(params);
        call.enqueue(new Callback<ApiResponseBody>() {
            @Override
            public void onResponse(Call<ApiResponseBody> call, Response<ApiResponseBody> response) {
                dismissProgress();
                if (response.isSuccessful()) {
                    startActivity(new Intent(getActivity(), FindWithDrawSuccessActivity.class));
                    getActivity().finish();
                } else {
                    showTost(response.raw().message(), 0);
                }
            }

            @Override
            public void onFailure(Call<ApiResponseBody> call, Throwable t) {
                dismissProgress();
                ApiException apiException = ExceptionHandle.handleException(t);
                if (apiException.isTokenInvalid()) {
                    showUnLoginSnackbar();
                } else if (apiException.isNetConnected()) {
                    showSetNetworkSnackbar();
                } else {
                    showTost(apiException.getMessage(), 1);
                }
            }
        });
    }

}
